package com.mobilerecognition.phonenumer.handler;

import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.mobilerecognition.engine.RecogResult;
import com.mobilerecognition.phonenumer.R;
import com.mobilerecognition.phonenumer.general.CGlobal;

import cn.bertsir.zbar.utils.QRUtils;

public final class RecogHandler extends Handler {

    private static final String TAG = RecogHandler.class.getSimpleName();

    private final RecogListener recogListener;


    RecogHandler(RecogListener recogListener) {
        this.recogListener = recogListener;
    }

    @Override
    public void handleMessage(Message message) {
        if (message.what == R.id.recog_start) {
            // Log.d(TAG, "Got decode message");
            decode((byte[]) message.obj, message.arg1, message.arg2);
        } else if (message.what == R.id.quit) {
            Looper.myLooper().quit();
        }
    }

    /**
     * @param data:   The YUV preview frame.
     * @param width:  The width of the preview frame.
     * @param height: The height of the preview frame.
     */
    private void decode(byte[] data, int width, int height) {
        recogListener.setIsAvailable(false);
        int rot = 90;
        long start = System.currentTimeMillis();
        int gap = 1;
        Rect orgRect = CGlobal.getOrgCropRect(width, height, rot, CGlobal.g_rectCrop);
        Bitmap recogBitmap = CGlobal.makeCropedGrayBitmap(data, width, height, rot, orgRect);
        RecogResult rawResult;
        if (recogListener.isQrCodeOne()) {
            rawResult = new RecogResult();
            try {
                String res = QRUtils.getInstance().decodeBarcode(recogBitmap);
                if (res != null && !res.equals("")) {
                    long end = System.currentTimeMillis();
                    rawResult.m_lineNumber = res;
                    rawResult.m_nRecogTime = (end - start);
                    postHandle(rawResult, recogBitmap);
                } else {
                    posHandleFail();
                }

            } catch (Exception e) {
                Log.i("SamTam", "error exception");
                e.printStackTrace();
                posHandleFail();
            }

        } else {
            rawResult = CGlobal.myEngine.RecogPhoneNumber_data(data, width, height, rot, orgRect);
            if (rawResult.m_nResultCount == 0) {
                rawResult = CGlobal.myEngineOld.RecogPhoneNumber_data(data, width, height, rot, orgRect);
            }
            long end = System.currentTimeMillis();
            rawResult.m_nRecogTime = (end - start);
            if (rawResult.m_nResultCount > 0) {
                postHandle(rawResult, recogBitmap);
            } else {
                posHandleFail();
            }
        }
        recogListener.setIsAvailable(true);

    }

    private void posHandleFail() {
        recogListener.setIsAvailable(true);
        Message message = Message.obtain(recogListener.getHandler(), R.id.recog_failed);
        message.sendToTarget();
    }

    private void postHandle(RecogResult rawResult, Bitmap recogBitmap) {
        recogListener.setIsAvailable(true);
        Message message = Message.obtain(recogListener.getHandler(), R.id.recog_succeeded, rawResult);
        Bundle bundle = new Bundle();
        bundle.putParcelable(CGlobal.PHONENUMBER_BITMAP, recogBitmap);
        message.setData(bundle);
        message.sendToTarget();

    }

}
